//package com.psbc.mq;
//
//import com.google.common.collect.Lists;
//import com.psbc.entity.BatchPullResult;
//import com.rabbitmq.client.*;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.io.IOException;
//import java.util.LinkedList;
//import java.util.concurrent.TimeoutException;
//
///**
// * @author:y022 21/06/2022 20:24
// * @description:
// */
////@Component
//@Slf4j
//public class RabbitMqConsumer   {
//    @Autowired
//    private ConnectionFactory connectionFactory;
//
//
////    @Scheduled(fixedDelay = 20000)
//    public void batchPullMsg() throws IOException, TimeoutException {
//        System.out.println("批量拉取消息启动");
//        Connection connection = connectionFactory.newConnection();
//        Channel channel = connection.createChannel();
////        Consumer consumer = new DefaultConsumer(channel) {
////            @Override
////            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
////                    throws IOException {
////                String s = new String(body, StandardCharsets.UTF_8);
////                list.add(s);
////                channel.basicAck(envelope.getDeliveryTag(), false);
////                return;
////            }
////        };
//        MyDeliverCallBack deliverCallback = new MyDeliverCallBack();
//
//        CancelCallback cancelCallback = s -> {
//            log.error("发生未知异常!");
//        };
//
//        channel.basicConsume("hello", false, deliverCallback,cancelCallback);
//        BatchPullResult result = deliverCallback.getResult();
//        try{
//            log.info("成功获取到消息:{}",result.getRes());
//            channel.basicAck(result.getOffset(),true);
//        }catch (Exception e){
//            channel.basicNack(result.getOffset(),true,true);
//        }
//    }
//
//    static class MyDeliverCallBack implements DeliverCallback{
//        LinkedList<String> list = Lists.newLinkedList();
//        long offset = 0L;
//        String  tag ;
//        @Override
//        public void handle(String s, Delivery delivery) {
//            if(list.size()<100){
//                list.add(new String(delivery.getBody()));
//                offset = delivery.getEnvelope().getDeliveryTag();
//                tag = s;
//            }
//        }
//        public BatchPullResult  getResult(){
//            return new BatchPullResult(this.list,this.offset,this.tag);
//        }
//    }
//
//}
